var historyStr = "";
var activeTab;

var blankDiv = document.getElementById("blankDiv");
var searchHistory = document.getElementById("searchHistory");
var searchInput = document.getElementById("searchInput");
var height = document.documentElement.clientHeight;
(function($) {

	setPxPerRem();

	mui.init({
		keyEventBind: {
			backbutton: true //开启back按键监听
		}
	});

	mui.plusReady(function() {
		plus.navigator.setStatusBarBackground('#FFFFFF');
		activeTab = getQueryString("activeTab");
		showDivByHistory();
		mui.back = function(event) {
			plus.navigator.setStatusBarBackground('#329ECF');
			var ws = plus.webview.currentWebview();
			plus.webview.close(ws);
			return false;
		};
	});

})(mui);

function showDivByHistory() {
	historyStr = localStorage.getItem("searchHistory")
	if(isEmpty(historyStr)) {
		showEle(blankDiv);
		hiddenEle(searchHistory)
	} else {
		hiddenEle(blankDiv)
		showEle(searchHistory);
		createSearchItems(historyStr);

	}
}
var searchInsert = document.getElementById("searchInsert");

function createSearchItems(str) {

	var items = str.split(",");
	if(items.length > 0) {
		for(var i = 0; i < items.length; i++) {
			var createLi = document.createElement("li");
			createLi.className = "mui-table-view-cell";
			createLi.innerHTML = items[i];
			document.body.appendChild(createLi);
			insertBefore(createLi, searchInsert);
		}
	}
	mui("#searchList").on('click', "li", function() {

		if(activeTab === "page_project.html") {
			searchProject(this.innerHTML)
		} else {
			searchResult(this.innerHTML);
		}
	});
}

function searchProject(keyword) {
	var postData = {
		pageflag: "page_search.html",
		height: height - 51,
		keyword: keyword
	}
	var change = {
		pageUrl: 'page_search_project.html',
		postData: postData,
		aniShow: "fade-in"
	}
	plus.navigator.setStatusBarBackground('#329ECF');
	pageChange(change);
}

function searchResult(keyword) {
	var postData = {
		pageflag: "page_search.html",
		keyword: keyword
	}
	var change = {
		pageUrl: 'page_search_result.html',
		postData: postData,
		aniShow: "fade-in"
	}
	plus.navigator.setStatusBarBackground('#329ECF');
	pageChange(change);
}

window.addEventListener('gotoBack', function(event) {
	var keyStr = event.detail.keyword;
	if(!isEmpty(keyStr)) {
		searchInput.value = "";
		searchInput.placeholder = keyStr;
	}
	showDivByHistory();
}, false);

document.getElementById("deleteBtn").addEventListener('click', function() {
	localStorage.setItem("searchHistory", "");
	showDivByHistory();
}, false);

document.getElementById("cancleBtn").onclick = function() {
	mui.back();
}

document.getElementById("submitIcon").onclick = function() {
	var keywordStr = searchInput.value;
	if(isEmpty(keywordStr)) {
		mui.toast("请输入楼宇名称或地址");
		return;
	}
	var setItemStr = keywordStr;
	if(!isEmpty(historyStr)) {
		setItemStr = historyStr + "," + setItemStr;
	}
	localStorage.setItem("searchHistory", setItemStr);
	if(activeTab === "page_project.html") {
		searchProject(keywordStr);
	} else {
		searchResult(keywordStr);
	}
}